$(function() {
	$("#repair_type_btn").click(function() {
		$.ajax({
			url:'repair_type/list',
			type:'GET',
			dataType:'json'
		}).done(function(data) {
			var html = [];
			html.push('<div class="modal-header">');
			html.push('	<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>');
			html.push('	<h4 class="modal-title" id="myModalLabel">수리유형</h4>');
			html.push('</div>');
			html.push('<div class="modal-body">');
			html.push('<label>조회조건</label> ')
			html.push('<input type="text" id="check_condition" size="30">')
			html.push('	<table class="table" id="repairTypeTable" data-click-to-select="true">');
			html.push('		<thead>');
			html.push('			<tr>');
			html.push('				<th></th>');
			html.push('				<th>수리코드</th>');
			html.push('				<th>수리명</th>');
			html.push('			</tr>');
			html.push('		</thead>');
			html.push('		<tbody>');
			
			if ( data != null && data.length > 0 ) {
				$.each(data, function(i, o) {
					html.push('				<tr>');
					html.push('					<td data-field="selectFlag"><input type="radio" name="selectRadio"></th>');
					html.push('					<td data-field="repairTypeCode">' + o['repairTypeCode'] + '</th>');
					html.push('					<td data-field="repairTypeName">' + o['repairTypeName'] + '</th>');
					html.push('				</tr>');				
				});
			}
			
			html.push('		</tbody>');
			html.push('	</table>');
			html.push('</div>');
			html.push('<div class="modal-footer">');
			html.push('<button type="button" class="btn btn-primary" id="confirm">확인</button>');
			html.push('<button type="button" class="btn btn-default" data-dismiss="modal">취소</button>');
			html.push('</div>');		
			
			$('div.modal-content').html(html.join(''));
			
			$('#confirm').on("click", function() {
				var selectedRepairTypeCode = "";
				var selectedRepairTypeName = "";

				var trList = $('#repairTypeTable tr');

				trList.each(function(i, e) {
					var checkFlag = $(e).find('input:radio').prop('checked');
					
					if ( checkFlag == true ) {
						selectedRepairTypeCode = $(e).children("td:nth(1)").text().trim();
						selectedRepairTypeName = $(e).children("td:nth(2)").text().trim();
						
						$('#repair_type_code').val(selectedRepairTypeCode);
						$('#repair_type_name').val(selectedRepairTypeName);
					}
				});
				
				$('#modalDiv').modal("hide");
			});
			
			$('tr').on("dblclick", function() {
				var selectedRepairTypeCode = $(this).children("td:nth(1)").text().trim();
				var selectedRepairTypeName = $(this).children("td:nth(2)").text().trim();
				
				$('#repair_type_code').val(selectedRepairTypeCode);
				$('#repair_type_name').val(selectedRepairTypeName);

				$('#modalDiv').modal("hide");
			});
			
			$('#modalDiv').modal("show");
		})
	});
});